package com.ruoyi.caipiao.utils;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * @author : wangcen  1020475339@qq.com
 * @description :
 * @date : 2022/11/1 19:52
 */

@Slf4j
public class MyFileUtil {
    public static String readFile(File fin) {
        StringBuilder sb = new StringBuilder();
        BufferedReader br = null;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(fin);

            br = new BufferedReader(new InputStreamReader(fis));

            String line;

            while ((line = br.readLine()) != null) {

                sb.append(line);

            }
        } catch (Exception e) {
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return sb.toString();

    }

    public static List<String> readFileAsList(File fin) {
        System.out.println(fin.getAbsolutePath());
        LinkedList<String> sb = new LinkedList<>();
        if(!fin.exists()) {
            return sb;
        }
        BufferedReader br = null;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(fin);

            br = new BufferedReader(new InputStreamReader(fis));

            String line;

            while ((line = br.readLine()) != null) {

                sb.add(line);

            }
        } catch (Exception e) {
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return sb;

    }

    public static Set<String> readFileToSet(File fin) {
        HashSet<String> sets = new HashSet<>();
        BufferedReader br = null;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(fin);

            br = new BufferedReader(new InputStreamReader(fis));

            String line;

            while ((line = br.readLine()) != null) {
                String replace = line.replace("[", "").replace("]", "").replaceAll(" ","");
                String[] split = replace.split(",");
                for (String s : split) {
                    if (StringUtils.isNotEmpty(s)) {
                        sets.add(s.trim());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(ExceptionUtil.getMessage(e));
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return sets;

    }

    public static Set<String> readFileToSet3(File fin) {
        HashSet<String> sets = new HashSet<>();
//        StringBuilder sb = new StringBuilder();
        BufferedReader br = null;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(fin);

            br = new BufferedReader(new InputStreamReader(fis));

            String line;

            while ((line = br.readLine()) != null) {
                String replace = line.replace("[", "").replace("]", "");
                String[] split = replace.split(",");
//                if(split.length < 4000) {
                for (String s : split) {
                    sets.add(s.trim());
                }
//                }
//                sb.append(line);

            }
        } catch (Exception e) {
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return sets;

    }

    public static Set<String> readFileToSet2(File fin) {
        HashSet<String> sets = new HashSet<>();
//        StringBuilder sb = new StringBuilder();
        BufferedReader br = null;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(fin);

            br = new BufferedReader(new InputStreamReader(fis));

            String line;

            while ((line = br.readLine()) != null) {
                String replace = line.replace("[", "").replace("]", "");
                String[] split = replace.split(",");
//                if(split.length < 4000) {
                for (String s : split) {
                    sets.add(s.trim());
                }
//                }
//                sb.append(line);
                break;
            }
        } catch (Exception e) {
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return sets;

    }

    public static void writeContentToFile(String file, String content) {
        FileWriter writer = null;
        try {
            writer = new FileWriter(file);
            writer.write(content);

            writer.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if(writer != null)
                writer.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static Set<String> readFileToSet4(File fin) {
        HashSet<String> sets = new HashSet<>();
        BufferedReader br = null;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(fin);

            br = new BufferedReader(new InputStreamReader(fis));

            String line;

            while ((line = br.readLine()) != null) {
                String replace = line.substring(line.indexOf("[")+1).replace("[", "").replace("]", "");//.replaceAll(" ", "");
                String[] split = replace.split(",");
                for (String s : split) {
                    if (StringUtils.isNotEmpty(s)) {
                        sets.add(s.trim());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(ExceptionUtil.getMessage(e));
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return sets;

    }

    public static void writeContentToFile(String file, String content, boolean append) {
        Writer writer = null;
        try {
            writer = new FileWriter(file, append);
            writer.write(content);
            writer.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public static void writeContentToFile(String file, String content, boolean append, boolean hh) {
        Writer writer = null;
        try {
            writer = new FileWriter(file, append);
            writer.write(content);
            if(hh) {
                writer.write(System.lineSeparator());
            }
            writer.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
